package com.beiing.tianshuai.tianshuai.huodong.model;

import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.entity.EventTypesBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.io.File;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Created by magikliu on 2017/10/22.
 */

public class HuoDongPublishModel implements IHuoDongPublishModel {

    private static final String TAG = "HuoDongPublishModel";
    private OnRequestResult mListener;

    public HuoDongPublishModel(OnRequestResult mListener) {
        this.mListener = mListener;
    }

    @Override
    public void getHuoDongType() {
        DisposableObserver<EventTypesBean> observer = new DisposableObserver<EventTypesBean>() {
            @Override
            public void onNext(@NonNull EventTypesBean huoDongTypeBean) {
                mListener.onGetTypeSuccess(huoDongTypeBean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                mListener.onError(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getEventTypes("0")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void getUploadImg(File file) {
        //构建body
        final RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("picname", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))
                .build();

        DisposableObserver<ResponseBody> observer = new DisposableObserver<ResponseBody>() {
            @Override
            public void onNext(@NonNull ResponseBody responseBody) {
                LogUtils.i(responseBody.toString(), TAG);
                mListener.onUploadSuccess(responseBody);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                mListener.onError(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getUploadActiveImgResult(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    public void publishActive(String strech, int tid, int uid, String sponsor, String action_time, String exit_time, String location, String lessons, String cover, String content, String contact, String tel, float much, int count, String create_time, int is_three, String three_url) {
        DisposableObserver<CodeBean> observable = new DisposableObserver<CodeBean>() {
            @Override
            public void onNext(@NonNull CodeBean codeBean) {
                mListener.onPublishSuccess(codeBean);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                mListener.onError(e);
            }

            @Override
            public void onComplete() {

            }
        };
        DataManager.getActivePublishResult(strech, tid, uid, sponsor, action_time, exit_time, location, lessons, cover, content, contact, tel, much, count, create_time, is_three, three_url)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observable);
    }

    public interface OnRequestResult{
        void onGetTypeSuccess(EventTypesBean typeBean);
        void onUploadSuccess(ResponseBody responseBody);
        void onPublishSuccess(CodeBean codeBean);
        void onError(Throwable throwable);
    }
}
